﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using System.Net.Mime;
using System.Net;

/// <summary>
/// Summary description for Mail
/// </summary>
public static class Mail
{
    private static AppSettingsReader settings = new AppSettingsReader();

    public static void EnviaCorreo(string texto, string file, string sDestino)
    {


        string mOrigen = (string)settings.GetValue("MailOrigen", typeof(string));
        string mDestino = sDestino; //(string)settings.GetValue("MailDestino", typeof(string));
        string mCC = (string)settings.GetValue("MailCC", typeof(string));
        string mSMTP = (string)settings.GetValue("SMTP", typeof(string));
        string mAsunto = (string)settings.GetValue("Asunto", typeof(string));
        string mPuerto = (string)settings.GetValue("Puerto", typeof(string));
        string mUserMail = (string)settings.GetValue("UserMail", typeof(string));
        string mPassMail = (string)settings.GetValue("PassMail", typeof(string));

        string mensaje = texto;

        try
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(mOrigen);
            mail.To.Add(mDestino);
            mail.CC.Add(mCC);
            mail.Subject = mAsunto;
            mail.Body = mensaje;

            //Se comento estas lineas porque ya no se va enviar como attachment el pdf
            // Create  the file attachment for this e-mail message.
            //Attachment data = new Attachment(file, MediaTypeNames.Application.Pdf);
            //// Add time stamp information for the file.
            //ContentDisposition disposition = data.ContentDisposition;
            //disposition.CreationDate = System.IO.File.GetCreationTime(file);
            //disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
            //disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
            //// Add the file attachment to this e-mail message.
            //mail.Attachments.Add(data);



            SmtpClient smtp = new SmtpClient(mSMTP, int.Parse(mPuerto));
            NetworkCredential SMTPUserInfo = new NetworkCredential(mUserMail, mPassMail);
            smtp.UseDefaultCredentials = false;
            smtp.EnableSsl = true;
            smtp.Credentials = SMTPUserInfo;
            smtp.Timeout = 500000;
            smtp.Send(mail);

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }


    }
}